package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.lang.Collections;
import org.example.exception.BaseException;
import org.example.mapper.SleepRecordMapper;
import org.example.model.SleepRecord;
import org.example.model.request.SleepRecordRequest;
import org.example.model.response.SleepRecordVO;
import org.example.service.SleepRecordService;
import org.example.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SleepRecordServiceImpl extends ServiceImpl<SleepRecordMapper, SleepRecord> implements SleepRecordService {

    /**
     * 上传睡眠记录
     *
     * @param sleepRecordRequest
     * @return
     */
    @Override
    public String uploadSleep(SleepRecordRequest sleepRecordRequest) {
        String openId = ThreadLocalUtil.getCurrentUser().getOpenid();
        //获取今日的00:00分
        LocalDateTime nowDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        //获取今日的23:59分
        LocalDateTime postponeDay = nowDay.withHour(23).withMinute(59).withSecond(59).withNano(0);
        SleepRecord sleepRecord = this.baseMapper.selectOne(new LambdaQueryWrapper<SleepRecord>()
                .eq(SleepRecord::getOpenId, openId)
                .le(SleepRecord::getCreateTime, postponeDay)
                .ge(SleepRecord::getCreateTime, nowDay));
        if(null != sleepRecord) throw new BaseException("今日已上传睡眠记录，请勿重复上传");
        sleepRecord = new SleepRecord();
        BeanUtils.copyProperties(sleepRecordRequest,sleepRecord);
        //获取今天是周几
        sleepRecord.setWeekDay((short) LocalDateTime.now().getDayOfWeek().getValue());
        sleepRecord.setOpenId(openId);
        sleepRecord.setSleepEfficiency(String.valueOf(sleepRecordRequest.getActualSleepTime() * 1.0 / sleepRecordRequest.getSleepTime()));
        if(this.baseMapper.insert(sleepRecord) > 0)
        {
            return "上传成功";
        }
        return "上传失败";
    }

    @Override
    public List<SleepRecordVO> getNowWeekSleepReport(Boolean moreFlag) {
        String openId = ThreadLocalUtil.getCurrentUser().getOpenid();
        LocalDateTime monday = null, now = null;
        if(!moreFlag)
        {
            //获取本周周一的00:00
            monday = LocalDateTime.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).withHour(0).withMinute(0).withSecond(0).withNano(0);
        }
        else
        {
            //本月1号00:00
            monday = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0).withNano(0);
        }
        //获取当前时间
        now = LocalDateTime.now();

        List<SleepRecord> sleepRecords = this.baseMapper.selectList(new LambdaQueryWrapper<SleepRecord>()
                .eq(SleepRecord::getOpenId, openId)
                .le(SleepRecord::getCreateTime, monday)
                .ge(SleepRecord::getActualSleepTime, now)
                .orderByAsc(SleepRecord::getId));
        if(!Collections.isEmpty(sleepRecords))
        {
            return sleepRecords.stream().map(sleepRecord -> {
                SleepRecordVO sleepRecordVO = new SleepRecordVO();
                BeanUtils.copyProperties(sleepRecord, sleepRecordVO);
                return sleepRecordVO;
            }).collect(Collectors.toList());
        }
        return new ArrayList<SleepRecordVO>();
    }
}
